package iso;
/*  
 *  JIIC: Java ISO Image Creator. Copyright (C) 2007, Jens Hatlak <hatlak@rbg.informatik.tu-darmstadt.de>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import plexus.PlexusCommandLine;
import de.tu_darmstadt.informatik.rbg.hatlak.iso9660.ConfigException;
import de.tu_darmstadt.informatik.rbg.hatlak.iso9660.ISO9660RootDirectory;
import de.tu_darmstadt.informatik.rbg.hatlak.iso9660.impl.CreateISO;
import de.tu_darmstadt.informatik.rbg.hatlak.iso9660.impl.ISO9660Config;
import de.tu_darmstadt.informatik.rbg.hatlak.joliet.impl.JolietConfig;
import de.tu_darmstadt.informatik.rbg.hatlak.rockridge.impl.RockRidgeConfig;
import de.tu_darmstadt.informatik.rbg.mhartle.sabre.HandlerException;
import de.tu_darmstadt.informatik.rbg.mhartle.sabre.StreamHandler;

public class ISOBurn {
	private static boolean enableJoliet = true;
	private static boolean enableRockRidge = true;

	private ISO9660RootDirectory root = new ISO9660RootDirectory();
	
	private File outfile;
	
	public ISOBurn(File outfile, List<File> files) throws HandlerException {
		this.outfile = outfile;
		
		for (File file : files) {
			if (file.exists()) {
				if (file.isDirectory()) {
					root.addContentsRecursively(file);
				} else {
					root.addFile(file);
				}
			}
		}

	}
	
	public void process(String publisher, String volumeId) throws ConfigException, FileNotFoundException, HandlerException{
		
		// ISO9660 support
		ISO9660Config iso9660Config = new ISO9660Config();
		iso9660Config.allowASCII(false);
		iso9660Config.setInterchangeLevel(1);
		iso9660Config.restrictDirDepthTo8(true);
		iso9660Config.setPublisher(publisher);
		iso9660Config.setVolumeID(volumeId);
		iso9660Config.setDataPreparer(publisher);
		// iso9660Config.setCopyrightFile(new File("Copyright.txt"));
		iso9660Config.forceDotDelimiter(true);

		RockRidgeConfig rrConfig = null;
		if (enableRockRidge) {
			// Rock Ridge support
			rrConfig = new RockRidgeConfig();
			rrConfig.setMkisofsCompatibility(false);
			rrConfig.hideMovedDirectoriesStore(true);
			rrConfig.forcePortableFilenameCharacterSet(true);
		}

		JolietConfig jolietConfig = null;
		if (enableJoliet) {
			// Joliet support
			jolietConfig = new JolietConfig();
			jolietConfig.setPublisher(publisher);
			jolietConfig.setVolumeID(volumeId);
			jolietConfig.setDataPreparer(publisher);
			jolietConfig.forceDotDelimiter(true);
		}

		// Create ISO
		StreamHandler streamHandler = new IsoHandler(outfile);

		// StreamHandler streamHandler = new ISOImageFileHandler(outfile);
		CreateISO iso = new CreateISO(streamHandler, root);
		iso.process(iso9660Config, rrConfig, jolietConfig, null);

		streamHandler.endDocument();
		
	}

	public static void main(String[] args) throws Exception {

		// Output file
		File outfile = new File("ISOTest.iso");

		System.out.println("Done. File is: " + outfile.getAbsolutePath());

		// streamHandler.endDocument();

		PlexusCommandLine commandLine = new PlexusCommandLine("isoburn");

		String out = commandLine.execute(outfile.getAbsolutePath());

		System.out.println("Out: " + out);
		System.out.println("Exit: " + commandLine.getExitCode());

		// Desktop.getDesktop().open(outfile);
	}
}